﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartCA.Model.Membership;
using System.Data;
using SmartCA.Infrastructure.Helpers;

namespace SmartCA.Infrastructure.Membership.Providers
{
    public static class ApplicationFactory
    {
        internal static class FieldNames
        {
            public const string ApplicationId = "ApplicationId";
            public const string ApplicationName = "ApplicationName";
            public const string EnablePasswordReset = "EnablePasswordReset";
            public const string EnablePasswordRetrieval = "EnablePasswordRetrieval";
            public const string MaxInvalidPasswordAttempts = "MaxInvalidPasswordAttempts";
            public const string MinRequiredNonAlphanumericCharacters = "MinRequiredNonAlphanumericCharacters";
            public const string MinRequiredPasswordLength = "MinRequiredPasswordLength";
            public const string PasswordAttemptWindow = "PasswordAttemptWindow";
            public const string PasswordFormat = "PasswordFormat";
            public const string PasswordStrengthRegularExpression = "PasswordStrengthRegularExpression";
            public const string RequiresQuestionAndAnswer = "RequiresQuestionAndAnswer";
            public const string RequiresUniqueEmail = "RequiresUniqueEmail";
            public const string IsDecryptionKeyAutogenerated = "IsDecryptionKeyAutogenerated";
            public const string IsHashAlgorithmFromMembershipConfig = "IsHashAlgorithmFromMembershipConfig";
            public const string HashAlgorithmType = "HashAlgorithmType";
            public const string MaxPasswordSize = "MaxPasswordSize";
            public const string MaxPasswordAnswerSize = "MaxPasswordAnswerSize";
            public const string MaxUsernameSize = "MaxUsernameSize";
            public const string MaxPasswordQuestionSize = "MaxPasswordQuestionSize";
        }

        public static Application BuildApplication(IDataReader reader)
        {
            return new Application(DataHelper.GetString(reader[FieldNames.ApplicationName]),
                DataHelper.GetBoolean(reader[FieldNames.EnablePasswordReset]),
                DataHelper.GetBoolean(reader[FieldNames.EnablePasswordRetrieval]),
                DataHelper.GetInteger(reader[FieldNames.MaxInvalidPasswordAttempts]),
                DataHelper.GetInteger(reader[FieldNames.MinRequiredNonAlphanumericCharacters]),
                DataHelper.GetInteger(reader[FieldNames.MinRequiredPasswordLength]),
                DataHelper.GetInteger(reader[FieldNames.PasswordAttemptWindow]),
                DataHelper.GetEnumValue<PasswordFormat>(reader[FieldNames.PasswordFormat]),
                DataHelper.GetString(reader[FieldNames.PasswordStrengthRegularExpression]),
                DataHelper.GetBoolean(reader[FieldNames.RequiresQuestionAndAnswer]),
                DataHelper.GetBoolean(reader[FieldNames.RequiresUniqueEmail]),
                DataHelper.GetBoolean(reader[FieldNames.IsDecryptionKeyAutogenerated]),
                DataHelper.GetBoolean(reader[FieldNames.IsHashAlgorithmFromMembershipConfig]),
                DataHelper.GetString(reader[FieldNames.HashAlgorithmType]),
                DataHelper.GetInteger(reader[FieldNames.MaxPasswordSize]),
                DataHelper.GetInteger(reader[FieldNames.MaxPasswordAnswerSize]),
                DataHelper.GetInteger(reader[FieldNames.MaxUsernameSize]),
                DataHelper.GetInteger(reader[FieldNames.MaxPasswordQuestionSize]));
        }
    }
}
